//
//  ZLColorWall.m
//  ZLColorWall
//
//  Created by 赵振磊 on 2024/3/15.
//

#import "ZLColorWall.h"
#import "ZLColorView.h"
#import "ZLColorSlider.h"
#import <Masonry/Masonry.h>
#import <ZLCategoryExpand/ZLCategory.h>

@interface ZLColorWall()
/// 内容视图
@property (nonatomic, strong) UIView *contentView;
/// 颜色视图
@property (nonatomic, strong) ZLColorView *colorView;
/// 颜色滑块
@property (nonatomic, strong) ZLColorSlider *slider;
/// 色块
@property (nonatomic, strong) UIView *showColorView;
/// 关闭
@property (nonatomic, strong) UIImageView *closeImage;
@end

@implementation ZLColorWall

+ (void)showIn:(UIViewController *)vc colorChange:(ZLColorWallChange)colorChange {
    ZLColorWall *wall = [[ZLColorWall alloc] init];
    wall.colorChange = colorChange;
    [vc presentViewController:wall animated:YES completion:nil];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
        self.view.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(500);
    }];
    [self.contentView addSubview:self.closeImage];
    [self.closeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.width.height.mas_equalTo(34);
        make.top.mas_equalTo(20);
    }];
    [self.contentView addSubview:self.showColorView];
    [self.showColorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.closeImage.mas_left).offset(-10);
        make.width.height.equalTo(self.closeImage.mas_height);
        make.centerY.equalTo(self.closeImage);
    }];
    [self.contentView addSubview:self.slider];
    [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(20);
        make.right.equalTo(self.showColorView.mas_left).offset(-10);
        make.height.equalTo(self.closeImage);
    }];
    [self.contentView addSubview:self.colorView];
    [self.colorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.equalTo(self.slider.mas_bottom).offset(20);
        make.height.mas_equalTo(300);
    }];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizerAction:)];
    [self.view addGestureRecognizer:pan];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeAction)];
    [self.view addGestureRecognizer:tap];
}

- (UIColor *)color {
    return [self.showColorView.backgroundColor colorWithAlphaComponent:self.showColorView.alpha];
}

#pragma mark - action

- (void)panGestureRecognizerAction:(UIPanGestureRecognizer *)pan {
    CGFloat y = MAX([pan translationInView:self.view].y, 0.0);
    if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
        if (y <= 150) {
            [UIView animateWithDuration:0.3 animations:^{
                self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
            }];
        } else {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    } else {
        self.view.frame = CGRectMake(0, y, self.view.frame.size.width, self.view.frame.size.height);
    }
}

- (void)closeAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - init

- (ZLColorView *)colorView {
    if (!_colorView) {
        __weak __typeof(self)weakSelf = self;
        _colorView = [[ZLColorView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 40, 300)];
        _colorView.cornerRadius = 5;
        _colorView.colorChange = ^(UIColor * _Nonnull color) {
            weakSelf.showColorView.backgroundColor = color;
            weakSelf.slider.color = color;
            weakSelf.closeImage.tintColor = color;
            if (weakSelf.colorChange) {
                weakSelf.colorChange(weakSelf.color);
            }
        };
    }
    return _colorView;
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 500)];
        _contentView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.8];
        [_contentView zl_cornersRadius:5 corners:UIRectCornerTopLeft | UIRectCornerTopRight];
        UIBlurEffect *beffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView *view = [[UIVisualEffectView alloc] initWithEffect:beffect];
        view.frame = _contentView.bounds;
        [_contentView addSubview:view];
    }
    return _contentView;
}

- (ZLColorSlider *)slider {
    if (!_slider) {
        __weak __typeof(self)weakSelf = self;
        _slider = [[ZLColorSlider alloc] initWithFrame:CGRectMake(20, 20, self.view.frame.size.width - 128, 34)];
        _slider.alphaChange = ^(CGFloat alpha) {
            weakSelf.showColorView.alpha = alpha;
            if (weakSelf.colorChange) {
                weakSelf.colorChange(weakSelf.color);
            }
        };
    }
    return _slider;
}

- (UIView *)showColorView {
    if (!_showColorView) {
        _showColorView = [[UIView alloc] init];
        _showColorView.backgroundColor = [UIColor whiteColor];
        _showColorView.zl_cornerRadius = 5;
    }
    return _showColorView;
}

- (UIImageView *)closeImage {
    if (!_closeImage) {
        _closeImage = [[UIImageView alloc] init];
        _closeImage.tintColor = [UIColor whiteColor];
        if (@available(iOS 13.0, *)) {
            _closeImage.image = [UIImage systemImageNamed:@"xmark.circle"];
        }
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeAction)];
        _closeImage.userInteractionEnabled = YES;
        [_closeImage addGestureRecognizer:tap];
    }
    return _closeImage;
}

@end

